package com.zhengke.service.area;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhengke.common.dao.CommonDao;
import com.zhengke.common.object.ZTreeNode;
import com.zhengke.pojo.SysArea;

@Service
public class AreaServiceImpl implements AreaService {
	
	@Autowired
	private CommonDao dao;
	
	@Override
	@SuppressWarnings("unchecked")
	public List<ZTreeNode> queryParent() {
		try {
			List<SysArea> areaList = (List<SysArea>) dao.findForList("areaDaoMapper.queryParent", null);
			List<ZTreeNode> rtnList = new ArrayList<ZTreeNode>();
			for(SysArea sysArea:areaList){
				ZTreeNode node = new ZTreeNode();
				node.setId(sysArea.getAreaId().toString());
				node.setName(sysArea.getAreaName());
				node.setIsParent(true);
				rtnList.add(node);
			}
			return rtnList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ZTreeNode> querySub(String id) {
		try {
			List<SysArea> areaList = (List<SysArea>)dao.findForList("areaDaoMapper.querySub", id);
			List<ZTreeNode> rtnList = new ArrayList<ZTreeNode>();
			for(SysArea sysArea:areaList){
				ZTreeNode node = new ZTreeNode();
				node.setId(sysArea.getAreaId().toString());
				node.setName(sysArea.getAreaName());
				int num = (int) dao.findForObject("areaDaoMapper.queryCount",sysArea);
				node.setIsParent(num>0);
				rtnList.add(node);
				
			}
			return rtnList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int insert(SysArea area) {
		try {
			int num = (int) dao.save("areaDaoMapper.insert", area);
			return num;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return 0;
	}

	@Override
	public SysArea queryByAreaId(String areaId) {
		try {
			SysArea sysArea = (SysArea) dao.findForObject("areaDaoMapper.queryByAreaId",areaId);
			return sysArea;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}

	@Override
	public int saveSysArea(SysArea sysArea) {
		try {
			int num = (int)dao.update("areaDaoMapper.saveSysArea", sysArea);
			return num;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int delAreaClass(String areaId) {
		
		try {
			return (int) dao.delete("areaDaoMapper.delAreaClass", areaId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

}
